package com.bilicraft.plugin.explode;

import java.io.File;
import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;

public class BiliExploder extends JavaPlugin{	
	@Override
    public void onEnable(){
		reloadConfiguration();
    	this.getLogger().info("BiliExploder started.");
    }
	@Override
    public void onDisable() {
    	this.getLogger().info("BiliExploder down.");
    }
	@Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    	if(cmd.getName().equalsIgnoreCase("explode")){
    		if(!(sender instanceof Player)){
    			sender.sendMessage("Can not executed by console.");
    			return false;
    		}
    		Player pl = (Player) sender;
    		ItemStack is = pl.getItemInHand();
    		ArrayList<ItemStack> ist = null;
    		if(is != null){
    			ist = toggle(is);
    		}else{
    			sender.sendMessage(BEConfig.nothing);
    			return false;
    		}
    		
    		if(ist != null){
    			pl.setItemInHand(new ItemStack(0, 0));
    			PlayerInventory pi = pl.getInventory();
    			for(ItemStack its:ist){
    				pi.addItem(its);
    			}
    			sender.sendMessage(BEConfig.success);
    		}else{
    			sender.sendMessage(BEConfig.invalid);
    		}
    		return true;
    	}
    	return false; 
    }
	
	public void reloadConfiguration(){
		if (!new File(getDataFolder(), "config.yml").exists()) {
			this.saveDefaultConfig();
		}
		reloadConfig();
		BEConfig.setAll(this.getConfig());
		for(String srs:BEConfig.exRecipe){
			if(addRecipe(srs)){
				this.getLogger().info("Add recipe "+srs);
			}else{
				this.getLogger().info("Invalid recipe "+srs);
			}
		}
	}
	
	public boolean addRecipe(String str){
		int amount = 1;
		int typeId = 0;
		int raw = 0;
		String[] s1 = str.split(":");
		String[] s3;
		if(s1.length != 2){
			return false;
		}
		String[] s2 = s1[0].split("x");
		s3 = s2[0].split("-");
		if(s3.length == 2){
			raw = Integer.valueOf(s3[1]);
		}
		typeId = Integer.valueOf(s3[0]);
		if(s2.length == 2){
			amount = Integer.valueOf(s2[1]);
		}
		s2 = s1[1].split(",");
		if(s2.length != 9){
			return false;
		}
		ShapedRecipe sr = new ShapedRecipe(new ItemStack(typeId, amount, (short) raw));
		char[] cs = {'0','1','2','3','4','5','6','7','8','9'};
		for(int i = 0; i < 9; i++){
			if(s2[i].indexOf("-")<0 && Integer.valueOf(s2[i]) == 0){
				cs[i] = ' ';
			}
		}
		String cs1 = String.valueOf(cs[0])+String.valueOf(cs[1])+String.valueOf(cs[2]);
		String cs2 = String.valueOf(cs[3])+String.valueOf(cs[4])+String.valueOf(cs[5]);
		String cs3 = String.valueOf(cs[6])+String.valueOf(cs[7])+String.valueOf(cs[8]);
		sr.shape(cs1, cs2, cs3);
		for(int i = 0; i < 9; i++){
			s3 = s2[i].split("-");
			if(s3.length == 2){
				sr.setIngredient(cs[i], Material.getMaterial(Integer.valueOf(s3[0])), Integer.valueOf(s3[1]));
			}else if(Integer.valueOf(s2[i]) != 0){
				sr.setIngredient(cs[i], Material.getMaterial(Integer.valueOf(s2[i])));
			}
		}
		return Bukkit.addRecipe(sr);	
	}
	
	public ArrayList<ItemStack> toggle(ItemStack _src){
		ArrayList<ItemStack> ist = new ArrayList<ItemStack>();
		String iss = BEConfig.exMap.get(_src.getTypeId());
		if(iss == null){
			return null;
		}
		String[] is1 = iss.split(",");
		for(String is2:is1){
			try{
				String[] is3 = is2.split("x");
				float ocRate = 1;
				if(is3.length > 2){
					ocRate = Float.valueOf(is3[2]);
				}
				if(Math.random() < ocRate){
					ist.add(new ItemStack(Integer.valueOf(is3[0]),Integer.valueOf(is3[1])*_src.getAmount()));
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return ist;
	}
}
